#include "setdialog.h"
#include "ui_setdialog.h"
#include <QFileDialog>

SetDialog::SetDialog(QWidget *parent,QString cfgString,QString weightString,QString nameString,double confidence) :
    QDialog(parent),
    ui(new Ui::SetDialog)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);
    connect(ui->exitButton,&QPushButton::clicked,[=](){
        this->close();
    });
    connect(ui->confidenceSlider,&QSlider::valueChanged,[=](){
        ui->confidenceSpinBox->setValue(double(ui->confidenceSlider->value())/100);
    });
    connect(ui->confidenceSpinBox,QOverload<double>::of(&QDoubleSpinBox::valueChanged),[=](double d){
        ui->confidenceSlider->setValue(d*100);
    });
    connect(ui->confirmButton,&QPushButton::clicked,[=](){
        emit changeCfg(ui->cfgStringLine->text());
        emit changeName(ui->namesStringLine->text());
        emit changeWeight(ui->weightsStringLine->text());
        emit changeConfidence(ui->confidenceSpinBox->value());
        emit ensure();
        this->close();
    });
    ui->cfgStringLine->setText(cfgString);
    ui->weightsStringLine->setText(weightString);
    ui->namesStringLine->setText(nameString);
    ui->confidenceSpinBox->setValue(confidence);
}

SetDialog::~SetDialog()
{
    delete ui;
}

void SetDialog::on_namesButton_clicked()
{
    QString file=QFileDialog::getOpenFileName(this,"选择类名",QCoreApplication::applicationDirPath()+"//res","*.names");
    if(file!="")
    {
        ui->namesStringLine->setText(file);
        emit changeName(file);
    }
}

void SetDialog::on_cfgButton_clicked()
{
    QString file=QFileDialog::getOpenFileName(this,"选择配置",QCoreApplication::applicationDirPath()+"//res","*.cfg");
    if(file!="")
    {
        ui->cfgStringLine->setText(file);
        emit changeCfg(file);
    }
}

void SetDialog::on_weightsButton_clicked()
{
    QString file=QFileDialog::getOpenFileName(this,"选择权重",QCoreApplication::applicationDirPath()+"//res","*.weights");
    if(file!="")
    {
        ui->weightsStringLine->setText(file);
        emit changeWeight(file);
    }
}
